package cn.schoolwow.flow.monitor.module.business.testcase.aware;

import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockListListener;
import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockTableField;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.module.router.domain.BlockRouterPush;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.entity.TestCase;
import cn.schoolwow.flow.monitor.module.business.testcase.flow.AddTestCaseButtonsFlow;
import cn.schoolwow.flow.monitor.module.business.testcase.flow.ResetTestCaseFlow;
import cn.schoolwow.flow.monitor.module.business.testcase.flow.select.AddTestCaseCategoryRemoteSelectFlow;
import cn.schoolwow.flow.monitor.module.common.domain.FlowMonitorConstant;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.domain.database.dql.response.PageVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class TestCaseAware extends EntityTableListBlockConfigAware {
    @Autowired
    private FlowMonitorOption flowMonitorOption;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public EntityTableListBlockOption getAMSBlockConfig() {
        flowMonitorOption.flowMonitorQuickFlow.startFlow(new ResetTestCaseFlow()).execute();

        EntityTableListBlockOption entityTableListBlockOption = new EntityTableListBlockOption(
                FlowMonitorConstant.PROJECT_NAME,"测试用例", FlowMonitorConstant.DAO_NAME, TestCase.class);
        entityTableListBlockOption.listBlockShowOption.initialSortField = "id";
        entityTableListBlockOption.listBlockShowOption.requestFieldNames = new String[]{"name"};

        addRouterPush(entityTableListBlockOption);
        setListListener(entityTableListBlockOption);

        //公共流程
        flowMonitorOption.flowRecordQuickFlow.startFlow(new AddTestCaseButtonsFlow())
                .next(new AddTestCaseCategoryRemoteSelectFlow())
                .putInstanceData(entityTableListBlockOption)
                .execute();
        return entityTableListBlockOption;
    }

    private void addRouterPush(EntityTableListBlockOption entityTableListBlockOption){
        {
            BlockRouterPush blockRouterPush = new BlockRouterPush("查看日志", applicationContext.getBean(TestCaseLogAware.class));
            blockRouterPush.sourceTargetFieldNameMap.put("id", "testcaseId");
            entityTableListBlockOption.blockRouterPushList.add(blockRouterPush);
        }
    }

    private void setListListener(EntityTableListBlockOption entityTableListBlockOption){
        entityTableListBlockOption.databaseListBlockListener.listListener = new DatabaseListBlockListListener() {
            @Override
            public void beforeExecuteCondition(Condition condition, JSONObject queryCondition) throws Exception {
                if(queryCondition.containsKey("categoryName")){
                    String categoryName = queryCondition.getString("categoryName");
                    queryCondition.put("categoryName", categoryName.replace(",", "-"));
                }

                Integer pass = queryCondition.getInteger("pass");
                if(null!=pass){
                    switch (pass){
                        case 1:{
                            condition.addQuery("pass", true);
                        }break;
                        case 2:{
                            condition.addQuery("pass", false);
                        }break;
                    }
                }
                queryCondition.remove("pass");
            }

            @Override
            public void afterExecuteCondition(Condition condition, JSONObject queryCondition, PageVo pageVo) throws Exception {

            }

            @Override
            public DatabaseListBlockTableField[] listTableFieldArray() {
                return new DatabaseListBlockTableField[]{
                        new DatabaseListBlockTableField("pass", "是否通过(0:全部,1:已通过,2:未通过)", "select", true)
                };
            }
        };
    }

}
